// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   RobotProgrammer.java

package jones.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.MouseInputAdapter;
import jones.SimpleRobot;
import jones.behavior.AntiMoth;
import jones.behavior.Avoid;
import jones.behavior.Behavior;
import jones.behavior.BehaviorFactory;
import jones.behavior.BehaviorFactoryException;
import jones.behavior.CollectionTask;
import jones.behavior.Cruise;
import jones.behavior.DarkPush;
import jones.behavior.Escape;
import jones.behavior.GDR;
import jones.behavior.Gizmo;
import jones.behavior.GizmoTask;
import jones.behavior.Home;
import jones.behavior.London;
import jones.behavior.LondonTask;
import jones.behavior.PriorityListArbiter;
import jones.behavior.Remote;
import jones.behavior.SimplePriorityListArbiter;
import jones.behavior.Stop;
import jones.behavior.Task;
import jones.behavior.UserControlTask;
import jones.behavior.WallFollow;
import jones.behavior.Wander;

// Referenced classes of package jones.gui:
//            PropertySheet

public class RobotProgrammer extends JDialog
{
    private class BehaviorMenuItem extends JMenuItem
    {

        Behavior taskBehavior = null;


        public BehaviorMenuItem(Behavior behavior)
        {
            super(behavior.getName());
            taskBehavior = behavior;
            addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    addToTask(taskBehavior);
                }

            }
);
        }
    }

    private class TaskMenuItem extends JMenuItem
    {

        private Behavior taskBehaviors[] = null;



        public TaskMenuItem(Task task)
        {
            super(task.getName());
            taskBehaviors = task.getBehaviors();
            addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    taskListModel.clear();
                    for (int i = 0; i < taskBehaviors.length; i++)
                        addToTask(taskBehaviors[i]);

                }

            }
);
        }
    }


    private Behavior behaviors[] = {
        new AntiMoth(), new Avoid(), new Cruise(), new DarkPush(), new Escape(), new GDR(), new Gizmo(), new Home(), new London(), new Remote(), 
        new WallFollow(), new Wander(), new Stop()
    };
    private Task tasks[] = {
        new CollectionTask(), new GizmoTask(), new LondonTask(), new UserControlTask()
    };
    private JList behaviorsList = null;
    private DefaultListModel taskListModel = null;
    private JList taskList = null;
    private SimpleRobot simpleRobot = null;
    private JPanel propertiesPanel = null;
    private JScrollPane propertiesScrollPane = null;
    private java.util.List taskBehaviors = null;
    private int progress = 0;
    private String progressNote = null;
    private boolean isCanceled = false;

    public RobotProgrammer(SimpleRobot robot)
    {
        behaviorsList = new JList(((Object []) (behaviors)));
        taskListModel = new DefaultListModel();
        taskList = new JList(((ListModel) (taskListModel)));
        propertiesPanel = new JPanel(((java.awt.LayoutManager) (new CardLayout())));
        propertiesScrollPane = new JScrollPane(((Component) (propertiesPanel)));
        taskBehaviors = ((java.util.List) (new ArrayList()));
        progress = 0;
        progressNote = "Loading . . .";
        isCanceled = false;
        simpleRobot = robot;
        setTitle("Robot Programmer");
        setResizable(false);
        setDefaultCloseOperation(2);
        setModal(true);
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu tasksMenu = new JMenu("Tasks");
        menuBar.add(tasksMenu);
        for (int i = 0; i < tasks.length; i++)
            tasksMenu.add(((JMenuItem) (new TaskMenuItem(tasks[i]))));

        JMenu behaviorMenu = new JMenu("Behaviors");
        menuBar.add(behaviorMenu);
        for (int i = 0; i < behaviors.length; i++)
            behaviorMenu.add(((JMenuItem) (new BehaviorMenuItem(behaviors[i]))));

        JPanel contentPane = new JPanel(((java.awt.LayoutManager) (new BorderLayout())));
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(((java.awt.LayoutManager) (new BoxLayout(((java.awt.Container) (buttonPanel)), 0))));
        buttonPanel.add(Box.createHorizontalGlue());
        JButton loadButton = new JButton("Load");
        loadButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                Behavior task[] = new Behavior[taskListModel.size()];
                taskListModel.copyInto(((Object []) (task)));
                simpleRobot.setBehaviors(task);
                simpleRobot.setArbiter(((jones.behavior.Arbiter) (new SimplePriorityListArbiter(((jones.SimpleRobotInterface) (simpleRobot)), task))));
                dispose();
            }

        }
);
        buttonPanel.add(((Component) (loadButton)));
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }

        }
);
        buttonPanel.add(((Component) (cancelButton)));
        contentPane.add(((Component) (buttonPanel)), "South");
        JPanel programmingPanel = new JPanel(((java.awt.LayoutManager) (new BorderLayout())));
        JPanel listPanel = new JPanel(((java.awt.LayoutManager) (new GridLayout(1, 2))));
        programmingPanel.add(((Component) (listPanel)), "Center");
        JPanel listButtonPanel = new JPanel(((java.awt.LayoutManager) (new GridLayout(1, 2))));
        programmingPanel.add(((Component) (listButtonPanel)), "South");
        javax.swing.ListCellRenderer renderer = ((javax.swing.ListCellRenderer) (new DefaultListCellRenderer() {

            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
            {
                JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                Behavior behavior = (Behavior)value;
                label.setToolTipText(behavior.getName());
                label.setText(behavior.getName());
                return ((Component) (label));
            }

        }
));
        behaviorsList.setCellRenderer(renderer);
        taskList.setCellRenderer(renderer);
        JScrollPane behaviorsScrollPane = new JScrollPane(((Component) (behaviorsList)));
        behaviorsScrollPane.setPreferredSize(new Dimension(150, 200));
        listPanel.add(((Component) (behaviorsScrollPane)));
        JScrollPane taskScrollPane = new JScrollPane(((Component) (taskList)));
        taskScrollPane.setPreferredSize(new Dimension(150, 200));
        listPanel.add(((Component) (taskScrollPane)));
        behaviorsList.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

            public void mouseClicked(MouseEvent e)
            {
                if (e.getClickCount() == 2)
                {
                    int index = behaviorsList.locationToIndex(e.getPoint());
                    if (index < 0)
                        return;
                    Behavior behavior = (Behavior)behaviorsList.getModel().getElementAt(index);
                    try
                    {
                        addToTask(BehaviorFactory.copy(behavior));
                    }
                    catch (BehaviorFactoryException ex)
                    {
                        JOptionPane.showMessageDialog(((Component) (RobotProgrammer.this)), ((Object) (ex)), "Behavior could not be added", 0);
                    }
                }
            }

        }
)));
        MouseInputAdapter mouseInputAdapter = ((MouseInputAdapter) (new MouseInputAdapter() {

            private Object value = null;
            private boolean isDragging = false;

            public void mousePressed(MouseEvent e)
            {
                int index = taskList.locationToIndex(e.getPoint());
                if (index < 0)
                {
                    return;
                } else
                {
                    value = taskListModel.getElementAt(index);
                    return;
                }
            }

            public void mouseDragged(MouseEvent e)
            {
                isDragging = true;
            }

            public void mouseReleased(MouseEvent e)
            {
                if (isDragging && value != null)
                {
                    int index = taskList.locationToIndex(e.getPoint());
                    if (index >= 0)
                    {
                        taskListModel.removeElement(value);
                        taskListModel.add(index, value);
                        taskList.setSelectedIndex(index);
                    }
                    value = null;
                    isDragging = false;
                }
            }

            public void mouseClicked(MouseEvent e)
            {
                if (e.getClickCount() == 2)
                {
                    int index = taskList.locationToIndex(e.getPoint());
                    if (index < 0)
                        return;
                    taskListModel.removeElementAt(index);
                }
            }

            
            {
                value = null;
                isDragging = false;
            }
        }
));
        taskList.addMouseListener(((java.awt.event.MouseListener) (mouseInputAdapter)));
        taskList.addMouseMotionListener(((java.awt.event.MouseMotionListener) (mouseInputAdapter)));
        taskList.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e)
            {
                JList list = (JList)e.getSource();
                int index = list.getSelectedIndex();
                if (index < 0 || index == taskListModel.size())
                {
                    return;
                } else
                {
                    Behavior behavior = (Behavior)taskListModel.getElementAt(index);
                    CardLayout cardLayout = (CardLayout)propertiesPanel.getLayout();
                    cardLayout.show(((java.awt.Container) (propertiesPanel)), "" + getTaskIndex(behavior));
                    propertiesScrollPane.getViewport().setViewPosition(new Point(0, 0));
                    return;
                }
            }

        }
);
        propertiesPanel.add(((Component) (new PropertySheet())), "Blank");
        PriorityListArbiter arbiter = (PriorityListArbiter)simpleRobot.getArbiter();
        Behavior currentTask[] = arbiter.getPriorityList();
        for (int i = 0; i < currentTask.length; i++)
            try
            {
                addToTask(BehaviorFactory.copy(currentTask[i]));
            }
            catch (BehaviorFactoryException ex)
            {
                JOptionPane.showMessageDialog(((Component) (this)), ((Object) (ex)), "Behavior could not be copied", 0);
            }

        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                Behavior behavior = (Behavior)behaviorsList.getSelectedValue();
                if (behavior == null)
                    return;
                try
                {
                    addToTask(BehaviorFactory.copy(behavior));
                }
                catch (BehaviorFactoryException ex)
                {
                    JOptionPane.showMessageDialog(((Component) (RobotProgrammer.this)), ((Object) (ex)), "Behavior could not be added", 0);
                }
            }

        }
);
        listButtonPanel.add(((Component) (addButton)));
        JButton removeButton = new JButton("Remove");
        removeButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                int index = taskList.getSelectedIndex();
                if (index < 0)
                {
                    return;
                } else
                {
                    taskListModel.removeElementAt(taskList.getSelectedIndex());
                    return;
                }
            }

        }
);
        listButtonPanel.add(((Component) (removeButton)));
        contentPane.add(((Component) (programmingPanel)), "Center");
        contentPane.add(((Component) (propertiesScrollPane)), "East");
        setContentPane(((java.awt.Container) (contentPane)));
        pack();
        show();
    }

    private void addToTask(Behavior behavior)
    {
        propertiesPanel.add(((Component) (new PropertySheet(((Object) (behavior))))), ((Object) ("" + taskBehaviors.size())));
        propertiesPanel.revalidate();
        propertiesPanel.repaint();
        taskBehaviors.add(((Object) (behavior)));
        taskListModel.addElement(((Object) (behavior)));
    }

    private int getTaskIndex(Behavior behavior)
    {
        for (int i = 0; i < taskBehaviors.size(); i++)
            if (taskBehaviors.get(i) == behavior)
                return i;

        return -1;
    }








}
